﻿/// <reference path="../../Context/js/jquery.min.min.js" />
/// <reference path="../..http://static.mpsoft.vn/Context/js/plugins/select2/select2.min.js" />
$(document).ready(function () {
    treeView("");
    SelectMenu();
});

function treeView(menuid) {
    // dynatree
    
}

function SelectMenu() {
    var menuSelect = "";

    if ($(".filetree").length > 0) {
        $(".filetree").each(function () {
            var $el = $(this),
                opt = {};
            opt.debugLevel = 0;
            //if ($el.hasClass("filetree-callbacks")) {
            //    opt.onActivate = function (node) {
            //        $(".activeFolder").text(node.data.title);
            //        console.log(menuSelect);
            //        //$(".additionalInformation").html("<ul style='margin-bottom:0;'><li>Key: " + node.data.key + "</li><li>is folder: " + node.data.isFolder + "</li></ul>");
            //    };
            //}
            opt.fx = { height: "toggle", duration: 200 };
            if ($el.hasClass("filetree-checkboxes")) {
                opt.checkbox = true;

                opt.onSelect = function (select, node) {
                    //console.log(node);
                    //console.log(select);
                    //var selNodes = node.tree.getSelectedNodes();
                    //var selKeys = $.map(selNodes, function (node) {
                    //    return "[" + node.data.key + "]: '" + node.data.title + "'";
                    //});
                    //$(".checkboxSelect").text(selKeys.join(", "));
                    if (menuSelect.trim().length < 1) {
                        $(node.span).removeClass("dynatree-selected");
                        alert("Bạn chọn vị trí menu!");
                        return false;
                    }
                };
            }
            opt.onExpand = function (flag, dtnode) {
                if (menuSelect.trim().length < 1) {
                    $(dtnode.span).removeClass("dynatree-selected");
                }
            }
            opt.onRender = function (dtnode, node) {
                //console.log(menuSelect);
                //console.log(dtnode);
                //if ($(node).hasClass(menuSelect)) {
                    //$(dtnode.span).addClass("dynatree-selected");
                    //dtnode.select(true);
                //}

            };
            opt.onClick = function (node, event) {
                
                if (menuSelect.trim().length > 0 && $(event.target).prop("class") == "dynatree-checkbox") {
                    if ($(node.span).hasClass(menuSelect)) {
                        //exists menu
                        if ($(node.span).hasClass("dynatree-selected"))
                            $(node.span).removeClass("dynatree-selected");
                        $(node.span).removeClass(menuSelect);
                        CallMethod("UpdateMenu", $.toJSON({ cateid: node.data.key, menuid: menuSelect }), function (data) {
                            console.log(menuSelect);
                        }, fail);
                    } else {
                        //not exists menu    
                        if (!$(node.span).hasClass("dynatree-selected"))
                            $(node.span).addClass("dynatree-selected");
                        $(node.span).addClass(menuSelect);
                        CallMethod("UpdateMenu", $.toJSON({ cateid: node.data.key, menuid: menuSelect }), function (data) {
                            console.log(menuSelect);
                        }, fail);
                    }
                } 
            };
            $el.dynatree(opt);
            $("#select-menu input").change(function () {
                if ($(this).prop("checked")) {
                    menuSelect = $(this).prop("id");
                    $el.dynatree("getRoot").visit(function (node) {
                        node.select(false);
                        if ($(node.span).hasClass(menuSelect))
                            node.select(true);
                        if (node.childList != null) {
                            node.expand(true);
                            $.each(node.childList, function (index, obj) {
                                obj.select(false);
                                if ($(obj.span).hasClass(menuSelect))
                                    obj.select(true);
                            });
                        }
                    });
                    loadCategories($(".select2-me").val(), menuSelect);
                    $("#title-dsmenu").html("3. Danh sách danh mục trong menu - " + $(this).parent().parent().text());
                }
            });
        });
    }
    $(".select2-me").change(function () {
        if (menuSelect.trim().length > 0)
            loadCategories($(this).val(), menuSelect);
        else 
            alert("Bạn chọn vị trí menu!");
        
    });
    $("#btn-refresh").click(function () {
        if (menuSelect.trim().length > 0)
            loadCategories($(".select2-me").val(), menuSelect);
        else
            alert("Bạn chọn vị trí menu!");
    });
}

function loadCategories(cateid, mnid) {
    CallMethod("CateMenu", $.toJSON({ parent: cateid, menuid: mnid }), function (data) {
        $(".table tbody").html(data.d);
        $('[rel=tooltip]').tooltip();
        upMenu(mnid);
    }, fail);
}
function upMenu(mnid) {
    $(".table .tb-text").blur(function () {
        CallMethod("UpOrder", $.toJSON({ cateid: $(this).attr("title"), order: $(this).val() }), function (d) {
            if (d.d > 0)
                console.log("ok");
        }, fail);
    });
    $(".table a.Delete").click(function (ev) {
        ev.preventDefault();
        CallMethod("UpdateMenu", $.toJSON({ cateid: $(this).attr("href"), menuid: mnid }), function (data) {
            if (data.d > 0)
                loadCategories($(".select2-me").val(), mnid);
        }, fail);
    });
}
function CallMethod(NameMethod, paramter, onSuccess, Fail) {
    $.ajax({
        type: "POST",
        url: _url + "/Services/Response.aspx/" + NameMethod,
        dataType: "json",
        data: paramter,
        contentType: "application/json; charset=utf-8",
        success: onSuccess,
        Error: Fail
    });
}

function fail(response) {
    alert("Có lỗi xảy ra yêu cầu làm lại.");
}